package com.fla.puzzle.ui;

import com.fla.puzzle.R;
import com.fla.puzzle.pack.PackItem;
import com.fla.puzzle.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * Shows 'Select Complexity Level' screen
 */
public class LevelSelectorActivity extends BaseActivity {
    public static final String ROWS_COUNT_KEY = LevelSelectorActivity.class.getName() + "ROWS_COUNT_KEY";
    public static final String COLS_COUNT_KEY = LevelSelectorActivity.class.getName() + "COLS_COUNT_KEY";
    public static final String ITEM_KEY = LevelSelectorActivity.class.getName() + "ITEM_KEY";

    private static final int[][] SMARTPHONE_TILES = {{4, 3}, {6, 4}, {8, 5}, {10, 6}};
    private static final int[][] TABLET_TILES = {{5, 3}, {8, 5}, {12, 9}, {15, 13}};

    private int[][] tiles;

    private TextView easyTiles;
    private TextView normalTiles;
    private TextView hardTiles;
    private TextView veryHardTiles;
    
    private PackItem selectedItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.level_selector_activity);

        easyTiles = (TextView) findViewById(R.id.easyTiles);
        normalTiles = (TextView) findViewById(R.id.normalTiles);
        hardTiles = (TextView) findViewById(R.id.hardTiles);
        veryHardTiles = (TextView) findViewById(R.id.veryHardTiles);

        tiles = Utils.isTabletDevice(this) ? TABLET_TILES : SMARTPHONE_TILES;
        
        selectedItem = (PackItem) getIntent().getSerializableExtra(ITEM_KEY);

        setTilesDependsDevice();
    }

    public static void startForResult(Activity activity, PackItem selectedItem, int requestCode) {
        Intent intent = new Intent(activity, LevelSelectorActivity.class);
        intent.putExtra(ITEM_KEY, selectedItem);
        activity.startActivityForResult(intent, requestCode);
    }

    private void setTilesDependsDevice() {
        setTilesToTextView(0, easyTiles);
        setTilesToTextView(1, normalTiles);
        setTilesToTextView(2, hardTiles);
        setTilesToTextView(3, veryHardTiles);
    }

    private void setTilesToTextView(int difficultyLevel, TextView textView) {
        int cols = tiles[difficultyLevel][0];
        int rows = tiles[difficultyLevel][1];
        int all = cols * rows;

        textView.setText(getString(R.string.tiles_template, all, cols, rows));
    }

    public void onEasyClicked(View view) {
        sendResult(0);
    }

    public void onNormalClicked(View view) {
        sendResult(1);
    }

    public void onHardClicked(View view) {
        sendResult(2);
    }
    
    public void onVeryHardClicked(View view) {
        sendResult(3);
    }

    private void sendResult(int difficultyLevel) {
        Intent data = new Intent();
        data.putExtra(COLS_COUNT_KEY, tiles[difficultyLevel][0]);
        data.putExtra(ROWS_COUNT_KEY, tiles[difficultyLevel][1]);
        data.putExtra(ITEM_KEY, selectedItem);
        setResult(RESULT_OK, data);
        finish();
    }
}
